package com.wwinsoft.framework.entity.security;

import java.util.List;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;
import javax.persistence.OneToMany;
import javax.persistence.OrderBy;
import javax.persistence.Table;
import javax.persistence.Transient;
import javax.persistence.Version;

import org.apache.commons.lang.builder.ToStringBuilder;
import org.codehaus.jackson.annotate.JsonIgnoreProperties;
import org.hibernate.annotations.Cache;
import org.hibernate.annotations.CacheConcurrencyStrategy;
import org.hibernate.annotations.Fetch;
import org.hibernate.annotations.FetchMode;
import org.hibernate.annotations.FilterDef;
import org.hibernate.annotations.FilterJoinTable;
import org.hibernate.annotations.GenericGenerator;
import org.hibernate.annotations.OnDelete;
import org.hibernate.annotations.OnDeleteAction;

import com.google.common.collect.Lists;
import com.wwinsoft.framework.entity.AssignedIDEntity;
import com.wwinsoft.framework.entity.AutoIdEntity;
import com.wwinsoft.modules.utils.reflection.ConvertUtils;

/**
 * 用户.
 * <p/>
 * 使用JPA annotation定义ORM关系. 使用Hibernate annotation定义JPA 1.0未覆盖的部分.
 *
 * @author calvin
 */
@Entity
//表名与类名不相同时重新定义表名.
@Table(name = "ACT_ID_USER")
//默认的缓存策略.
@Cache(usage = CacheConcurrencyStrategy.READ_WRITE)
@JsonIgnoreProperties(value = { "groupList", "roleNames", "handler", "hibernateLazyInitializer" })
@FilterDef(name = "defaultGroupFilter")
public class User extends AutoIdEntity {

    private String userId;
    private String password;
    private String firstName;
    private String lastName;
    private String email;
    private List<Group> groupList = Lists.newArrayList();
    private List<MemberShip> memberShipList = Lists.newArrayList();
    private Long revision;
    private Long deptId;
    private String jobTitle;
    private String telephone;
    private String ext;
    private String cellPhone;
    private String searchName;
    private String address;
    private boolean enabled;

    private List<Long> uiGroupIds = Lists.newArrayList();

    @Column(name = "ID_")
    public String getUserId() {
        return userId;
    }

    public void setUserId(String userId) {
        this.userId = userId;
    }

    @Column(name = "REV_")
    @Version
    public Long getRevision() {
        return revision;
    }

    public void setRevision(Long revision) {
        this.revision = revision;
    }

    @Column(name = "PWD_")
    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    @Column(name = "FIRST_")
    public String getFirstName() {
        return firstName;
    }

    public void setFirstName(String firstName) {
        this.firstName = firstName;
    }

    @Column(name = "LAST_")
    public String getLastName() {
        return lastName;
    }

    public void setLastName(String lastName) {
        this.lastName = lastName;
    }

    @Column(name = "EMAIL_")
    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    @Column(name = "DEPT_ID")
    public Long getDeptId() {
        return deptId;
    }

    public void setDeptId(Long deptId) {
        this.deptId = deptId;
    }

    public String getJobTitle() {
        return jobTitle;
    }

    public void setJobTitle(String jobTitle) {
        this.jobTitle = jobTitle;
    }

    public String getTelephone() {
        return telephone;
    }

    public void setTelephone(String telephone) {
        this.telephone = telephone;
    }

    public String getExt() {
        return ext;
    }

    public void setExt(String ext) {
        this.ext = ext;
    }

    public String getCellPhone() {
        return cellPhone;
    }

    public void setCellPhone(String cellPhone) {
        this.cellPhone = cellPhone;
    }

    public String getSearchName() {
        return searchName;
    }

    public void setSearchName(String searchName) {
        this.searchName = searchName;
    }

    public String getAddress() {
        return address;
    }

    public void setAddress(String address) {
        this.address = address;
    }

    public boolean isEnabled() {
        return enabled;
    }

    public void setEnabled(boolean enabled) {
        this.enabled = enabled;
    }

    //多对多定义
    @ManyToMany
    @FilterJoinTable(name = "defaultGroupFilter", condition = "type = 0")
    //中间表定义,表名采用默认命名规则
    @JoinTable(name = "ACT_ID_MEMBERSHIP", joinColumns = { @JoinColumn(name = "USER_ID_", referencedColumnName="ID_") },
               inverseJoinColumns = { @JoinColumn(name = "GROUP_ID_", referencedColumnName="ID_") })
    //Fecth策略定义
    @Fetch(FetchMode.SUBSELECT)
    //集合按id排序.
    @OrderBy("id")
    public List<Group> getGroupList() {
        return groupList;
    }

    public void setGroupList(List<Group> groupList) {
        this.groupList = groupList;
    }


    @OneToMany(cascade = { CascadeType.MERGE}, fetch = FetchType.LAZY)
    @JoinColumn(name = "USER_ID_", referencedColumnName="ID_")
    public List<MemberShip> getMemberShipList() {
        return memberShipList;
    }

    public void setMemberShipList(List<MemberShip> memberShipList) {
        this.memberShipList = memberShipList;
    }


    @Transient
    public List<Group> getDeptList() {
        List<Group> roleList = Lists.newArrayList();
        for (Group group : groupList) {
            if (Group.TYPE_DEPT.equals(group.getType())) {
                roleList.add(group);
            }
        }
        return roleList;
    }

    public void setDeptList(List<Group> deptList) {
        groupList.addAll(deptList);
    }

    @Transient
    public List<Group> getRoleList() {
        List<Group> roleList = Lists.newArrayList();
        for (Group group : groupList) {
            if (Group.TYPE_ROLE.equals(group.getType())) {
                roleList.add(group);
            }
        }
        return roleList;
    }

    public void setRoleList(List<Group> roleList) {
        groupList.addAll(roleList);
    }

    /**
     * 用户拥有的角色名称字符串, 多个角色名称用','分隔.
     */
    //非持久化属性.
    @Transient
    public String getRoleNames() {
        return ConvertUtils.convertElementPropertyToString(getRoleList(), "name", ", ");
    }

    @Transient
    public String getDeptNames() {
        return ConvertUtils.convertElementPropertyToString(getDeptList(), "name", ", ");
    }

    /**
     * 用户拥有的角色id字符串, 多个角色id用','分隔.
     */
    //非持久化属性.
    @Transient
    @SuppressWarnings("unchecked")
    public List<Long> getRoleIds() {
        return ConvertUtils.convertElementPropertyToList(getRoleList(), "id");
    }

    @Transient
    @SuppressWarnings("unchecked")
    public List<Long> getDeptIds() {
        return ConvertUtils.convertElementPropertyToList(getDeptList(), "id");
    }

    @Transient
    public List<Long> getUiGroupIds() {
        return uiGroupIds;
    }

    public void setUiGroupIds(List<Long> uiGroupIds) {
        this.uiGroupIds = uiGroupIds;
    }

    @Override
    public String toString() {
        return ToStringBuilder.reflectionToString(this);
    }
}